package br.urca.impl;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.AproveitamentoDAO;
import br.urca.model.Aproveitamento;

@Transactional
public class JPAAproveitamentoDAO implements AproveitamentoDAO {
	
	public static final String COD_DISCI_PRATICA_COMP_CURR = "EF119";
	
	private EntityManager manager;
	
	public JPAAproveitamentoDAO() {
		this(null);
	}
	
	@Inject
	public JPAAproveitamentoDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Aproveitamento> getListByMatricula(String matricula) {
		try {
			return this.manager
					.createQuery(
							"SELECT a FROM Aproveitamento a "
									+ "WHERE a.id.matricula = :matricula ",
							Aproveitamento.class)
					.setParameter("matricula", matricula).getResultList();
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<Aproveitamento> getDiscParaHistorico(String matricula) {
		List<String> situacoes = Arrays.asList("FC089", "EF119");
		try {
			return this.manager
					.createQuery(
							"SELECT a FROM Aproveitamento a "
									+ "WHERE a.id.matricula = :matricula "
									+ "AND a.id.codDisciplina not in (:situacoes) ",
							Aproveitamento.class)
					.setParameter("matricula", matricula)
					.setParameter("situacoes", situacoes).getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<Aproveitamento> getDiscParaHistoricoPorOferta(String oferta,
			String matricula) {
		List<String> situacoes = Arrays.asList("FC089", "EF119", "");
		try {
			return this.manager
					.createQuery(
							"SELECT a FROM Aproveitamento a "
									+ "WHERE a.id.matricula = :matricula "
									+ "AND a.oferta = :oferta "
									+ "AND a.id.codDisciplina not in (:situacoes) ",
							Aproveitamento.class)
					.setParameter("matricula", matricula)
					.setParameter("oferta", oferta)
					.setParameter("situacoes", situacoes).getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	

}
